// pages/plan/plan.js - 严格按照test示例设计规范
Page({
  data: {
    showModal: false,
    planName: '',
    frequencies: ['每周1次', '每周2次', '每周3次', '每周4次', '每周5次', '每周6次', '每天'],
    frequencyIndex: 0,
    durations: ['10分钟以内', '10-20分钟', '20-30分钟', '30-45分钟', '45分钟以上'],
    durationIndex: 0,
    targets: ['减脂塑形', '增肌', '核心力量', '柔韧性提升', '心肺功能', '综合训练'],
    targetIndex: 0,
    plans: [
      {
        id: 1,
        name: '减脂塑形计划',
        schedule: '每周一、三、五 · 45分钟/次',
        status: 'active',
        progress: 40,
        current: 12,
        total: 30
      },
      {
        id: 2,
        name: '核心力量训练',
        schedule: '每周二、四 · 30分钟/次',
        status: 'paused',
        progress: 40,
        current: 8,
        total: 20
      }
    ]
  },

  onLoad() {
    console.log('健身计划页面加载完成');
  },

  // 显示添加计划弹窗
  showAddPlanModal() {
    this.setData({
      showModal: true
    });
  },

  // 隐藏添加计划弹窗
  hideAddPlanModal() {
    this.setData({
      showModal: false
    });
  },

  // 阻止事件冒泡
  stopPropagation() {
    // 阻止点击弹窗内容时关闭弹窗
  },

  // 计划名称输入
  onPlanNameInput(e) {
    this.setData({
      planName: e.detail.value
    });
  },

  // 训练频率选择
  onFrequencyChange(e) {
    this.setData({
      frequencyIndex: e.detail.value
    });
  },

  // 训练时长选择
  onDurationChange(e) {
    this.setData({
      durationIndex: e.detail.value
    });
  },

  // 训练目标选择
  onTargetChange(e) {
    this.setData({
      targetIndex: e.detail.value
    });
  },

  // 创建计划
  createPlan() {
    const { planName, frequencies, frequencyIndex, durations, durationIndex, targets, targetIndex } = this.data;
    
    if (!planName.trim()) {
      wx.showToast({
        title: '请输入计划名称',
        icon: 'none'
      });
      return;
    }

    const newPlan = {
      id: Date.now(),
      name: planName,
      schedule: `${frequencies[frequencyIndex]} · ${durations[durationIndex]}`,
      status: 'active',
      progress: 0,
      current: 0,
      total: 30
    };

    this.setData({
      plans: [newPlan, ...this.data.plans],
      showModal: false,
      planName: ''
    });

    wx.showToast({
      title: '计划创建成功',
      icon: 'success'
    });
  },

  // 开始训练
  startTraining() {
    wx.navigateTo({
      url: '/pages/course/course?type=training'
    });
  },

  // 重新开始训练
  restartTraining() {
    wx.showToast({
      title: '重新开始训练',
      icon: 'success'
    });
  }
});
